package com.hjqjl.whapplib.base;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.viewbinding.ViewBinding;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.hjqjl.common_lib.base.ComLibActivity;
import com.hjqjl.common_lib.utils.ScreenUtils;
import com.hjqjl.whapplib.R;
import com.hjqjl.whapplib.utils.SPManager;
import com.mp5a5.www.library.utils.ApiConfig;

public class BaseActivity<VB extends ViewBinding> extends ComLibActivity<VB> {
    private InvalidTokenReceiver mInvalidTokenReceiver;
    private IntentFilter mInvalidTokenFilter;//必须配置ApiConfig的广播，否则空指针异常
    private boolean isInvalidTokenReceiver = false;
    private QuitAppReceiver mQuitAppReceiver;
    private IntentFilter mQuitAppFilter;
    private boolean isQuitAppReceiver = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initBlackWhite();

        if (ApiConfig.getTokenInvalidBroadcastFilter() != null) {
            isInvalidTokenReceiver = true;
            //联网token失效广播注册
            mInvalidTokenReceiver = new InvalidTokenReceiver();
            mInvalidTokenFilter = new IntentFilter();
            mInvalidTokenFilter.addAction(ApiConfig.getTokenInvalidBroadcastFilter());
        } else {
            isInvalidTokenReceiver = false;
        }

        if (ApiConfig.getQuitBroadcastFilter() != null) {
            isQuitAppReceiver = true;
            //联网退出广播注册
            mQuitAppReceiver = new QuitAppReceiver();
            mQuitAppFilter = new IntentFilter();
            mQuitAppFilter.addAction(ApiConfig.getQuitBroadcastFilter());
        } else {
            isQuitAppReceiver = false;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (isInvalidTokenReceiver) {
            registerReceiver(mInvalidTokenReceiver, mInvalidTokenFilter);
        }
        if (isQuitAppReceiver) {
            registerReceiver(mQuitAppReceiver, mQuitAppFilter);
        }
    }

    @Override
    protected void onPause() {//不是最上面的界面 会注销广播
        super.onPause();
        if (isInvalidTokenReceiver) {
            unregisterReceiver(mInvalidTokenReceiver);
        }
        if (isQuitAppReceiver) {
            unregisterReceiver(mQuitAppReceiver);
        }
    }

    /**
     * 设置标题名字
     */
    public void setTitle(String title) {
        setTitle(title, true);
    }

    /**
     * 设置标题名字
     */
    public void setTitle(String title, boolean isVisible) {
        TextView tv_toolbar_title = findViewById(R.id.tv_toolbar_title);
        if (tv_toolbar_title != null) {
            tv_toolbar_title.setText(title);
            ivTitleBack(isVisible);//设置标题的时候把返回按钮初始化
        }
    }

    /**
     * 实现标题返回的点击事件---:此点击事件可自定义
     */
    private void ivTitleBack(boolean isVisible) {
        ImageView iv_toolbar_back = findViewById(R.id.iv_toolbar_back);
        if (iv_toolbar_back != null) {
            if (isVisible) {
                iv_toolbar_back.setVisibility(View.VISIBLE);
                iv_toolbar_back.setOnClickListener(v -> backListener());
            } else {
                iv_toolbar_back.setVisibility(View.GONE);
            }
        }
    }

    /**
     * Title返回按钮点击事件（如需要setResult()时，在子类重写此方法，覆盖）
     */
    public void backListener() {
        finish();
    }

    /**
     * 初始化黑白屏
     */
    private void initBlackWhite() {
        boolean isBlackWhite = SPManager.hasBlackWhite();
        if (isBlackWhite) {
            ScreenUtils.setBlackWhite(this, 0);
        } else {
            ScreenUtils.setBlackWhite(this, 1);//需要重启app才可恢复彩色
        }
    }

    /**
     * token失效广播
     */
    private class InvalidTokenReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (ApiConfig.getTokenInvalidBroadcastFilter().equals(intent.getAction())) {
                String msg = intent.getStringExtra(ApiConfig.TokenType.TOKEN_INVALID_TAG.getName());
                if (!TextUtils.isEmpty(msg)) {
                    ToastUtils.showShort("token失效");
                    LogUtils.v(pSimpleName + "-->我收到InvalidTokenReceiver广播了--->" + msg);
                }
            }
        }
    }

    /**
     * 退出登录广播
     */
    private class QuitAppReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (ApiConfig.getQuitBroadcastFilter().equals(intent.getAction())) {
                String msg = intent.getStringExtra(ApiConfig.QuitType.QUIT_APP_TAG.getName());
                if (!TextUtils.isEmpty(msg)) {

                    LogUtils.v(pSimpleName + "-->我收到QuitAppReceiver广播了--->" + msg);
                }
            }
        }
    }
}
